package cn.iocoder.yudao.module.infrastructure.sms.service;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.infrastructure.sms.enums.SmsReceiveStatusEnum;
import cn.iocoder.yudao.module.api.infrastructure.sms.enums.SmsSendStatusEnum;
import cn.iocoder.yudao.module.infrastructure.sms.controller.admin.vo.log.SmsLogPageReqVO;
import cn.iocoder.yudao.module.infrastructure.sms.dal.dataobject.SmsLogDO;
import cn.iocoder.yudao.module.infrastructure.sms.dal.dataobject.SmsTemplateDO;
import cn.iocoder.yudao.module.infrastructure.sms.dal.mysql.SmsLogMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

/**
 * 短信日志 Service 实现类
 *
 * @author zzf
 */
@Slf4j
@Service
public class SmsLogServiceImpl implements SmsLogService {

    @Resource
    private SmsLogMapper smsLogMapper;

    @Override
    public Long createSmsLog(String mobile, Long userId, Integer userType, Boolean isSend,
                             SmsTemplateDO template, String templateContent, Map<String, Object> templateParams) {
        SmsLogDO smsLogDO = new SmsLogDO();
        // 根据是否要发送，设置状态
        smsLogDO.setSendStatus(Objects.equals(isSend, true) ? SmsSendStatusEnum.INIT.getStatus()
                : SmsSendStatusEnum.IGNORE.getStatus());
        // 设置手机相关字段
        smsLogDO.setMobile(mobile);
        smsLogDO.setUserId(userId);
        smsLogDO.setUserType(userType);
        // 设置模板相关字段
        smsLogDO.setTemplateId(template.getId());
        smsLogDO.setTemplateCode(template.getCode());
        smsLogDO.setTemplateType(template.getType());
        smsLogDO.setTemplateContent(templateContent);
        smsLogDO.setTemplateParams(templateParams);
        smsLogDO.setApiTemplateId(template.getApiTemplateId());
        // 设置渠道相关字段
        smsLogDO.setChannelId(template.getChannelId());
        smsLogDO.setChannelCode(template.getChannelCode());
        // 设置接收相关字段
        smsLogDO.setReceiveStatus(SmsReceiveStatusEnum.INIT.getStatus());
        smsLogMapper.insert(smsLogDO);
        return smsLogDO.getId();
    }

    @Override
    public void updateSmsSendResult(Long id, Boolean success,
                                    String apiSendCode, String apiSendMsg,
                                    String apiRequestId, String apiSerialNo) {
        SmsLogDO smsLogDO = new SmsLogDO();
        smsLogDO.setId(id);
        smsLogDO.setSendStatus(success ? SmsSendStatusEnum.SUCCESS.getStatus() : SmsSendStatusEnum.FAILURE.getStatus());
        smsLogDO.setSendTime(LocalDateTime.now());
        smsLogDO.setApiSendCode(apiSendCode);
        smsLogDO.setApiSendMsg(apiSendMsg);
        smsLogDO.setApiRequestId(apiRequestId);
        smsLogDO.setApiSerialNo(apiSerialNo);
        smsLogMapper.updateById(smsLogDO);
    }

    @Override
    public void updateSmsReceiveResult(Long id, Boolean success, LocalDateTime receiveTime,
                                       String apiReceiveCode, String apiReceiveMsg) {
        SmsReceiveStatusEnum receiveStatus = Objects.equals(success, true) ?
                SmsReceiveStatusEnum.SUCCESS : SmsReceiveStatusEnum.FAILURE;
        SmsLogDO smsLogDO = new SmsLogDO();
        smsLogDO.setId(id);
        smsLogDO.setReceiveStatus(receiveStatus.getStatus());
        smsLogDO.setReceiveTime(receiveTime);
        smsLogDO.setApiReceiveCode(apiReceiveCode);
        smsLogDO.setApiReceiveMsg(apiReceiveMsg);
        smsLogMapper.updateById(smsLogDO);
    }

    @Override
    public PageResult<SmsLogDO> getSmsLogPage(SmsLogPageReqVO pageReqVO) {
        return smsLogMapper.selectPage(pageReqVO);
    }

}
